<?php
/**
 * DecoComp controller
 */
namespace App\Http\Controllers;

use App\Service\DecoCompService;
use App\Validation\ListValidation;

class DecoCompController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否

	/**
	 * [indexAction 房源列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser(0);
		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$data  = DecoCompService::getList($raw);

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$d[$key]->go_url = $value->go_url ? $value->go_url : $this->config->gourl . $this->config->urls->deco_comp . $value->id;
			//第三方url
			$d[$key]->three_url = $d[$key]->go_url;
			$d[$key]->favor_rate = $value->favor_rate ? $value->favor_rate : "99.95%";
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->deco_comp . $value->id;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
		}

		$count = DecoCompService::getList($raw, TRUE);
		$assign = [
			'total' => $count,	
			'list'  => $d,
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 房源详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();

		if (isset($this->user)){
			$data = DecoCompService::getInfo($id, $this->user->member_id);
		}else{
			$data = DecoCompService::getInfo($id);
		}
		if ($data){
			//判断缩略图是否有图且url是否完整
			$data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->go_url =  $data->go_url ? $data->go_url : $this->config->gourl . $this->config->urls->deco_comp . $data->id;
			//第三方url
			$data->three_url = $data->go_url;
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->sources = $data->sources ? $data->sources : "觅窝";
		}

		return $this->echoJson($data);
	}

}